//------------------------------------------------------------------------------
// Copyright (c) 2008 www.dnaide.com
// Licensed under the MIT (MIT-LICENSE.txt)
//------------------------------------------------------------------------------
using System;
using System.Globalization;

namespace DNAide.Core
{
	public static partial class CastAide
	{

		/// <summary>
		/// Returns an object as a String.
		/// </summary>
		/// <param name="value">Value to cast</param>
		/// <returns>String</returns>
		/// <remarks>
		/// <para>
		/// Will return String.Empty if the cast fails.
		/// </para>
		/// <para>
		/// Uses CultureInfo.CurrentCulture.
		/// </para>
		/// </remarks>
		public static String AsString(object value)
		{
			return AsString(value, string.Empty, CultureInfo.CurrentCulture);
		}


		/// <summary>
		/// Returns an object as a String.
		/// </summary>
		/// <param name="value">Value to cast</param>
		/// <param name="defaultValue">Value returned if cast fails</param>
		/// <returns>String</returns>
		/// <remarks>
		/// <para>
		/// Uses CultureInfo.CurrentCulture.
		/// </para>
		/// </remarks>
		public static String AsString(object value, string defaultValue)
		{
			return AsString(value, defaultValue, CultureInfo.CurrentCulture);
		}


		/// <summary>
		/// Returns an object as a String.
		/// </summary>
		/// <param name="value">Value to cast</param>
		/// <param name="provider">Instance of IFormatProvider</param>
		/// <returns>String</returns>
		/// <remarks>
		/// <para>
		/// Will return String.Empty if the cast fails.
		/// </para>
		/// </remarks>
		public static String AsString(object value, IFormatProvider provider)
		{
			return AsString(value, string.Empty, provider);
		}


		/// <summary>
		/// Returns an object as a String.
		/// </summary>
		/// <param name="value">Value to cast</param>
		/// <param name="defaultValue">Value returned if cast fails</param>
		/// <param name="provider">Instance of IFormatProvider</param>
		/// <returns>String</returns>
		public static String AsString(object value, string defaultValue, IFormatProvider provider)
		{
			if (value == null || value == System.DBNull.Value)
				return defaultValue;

			if (provider == null)
				provider = CultureInfo.CurrentCulture;

			String retval;

			try
			{
				retval = Convert.ToString(value, provider);
			}
			catch (InvalidCastException)
			{
				retval = defaultValue;
			}
			return retval;
		}


		/// <summary>
		/// Returns an object as a String using the invariant culture.
		/// </summary>
		/// <param name="value">Value to cast</param>
		/// <returns>String</returns>
		/// <remarks>
		/// <para>
		/// Will return String.Empty if the cast fails.
		/// </para>
		/// </remarks>
		public static String AsStringInvariant(object value)
		{
			return AsString(value, string.Empty, CultureInfo.InvariantCulture);
		}


		/// <summary>
		/// Returns an object as a String using the invariant culture.
		/// </summary>
		/// <param name="value">Value to cast</param>
		/// <param name="defaultValue">Value returned if cast fails</param>
		/// <returns>String</returns>
		public static String AsStringInvariant(object value, string defaultValue)
		{
			return AsString(value, defaultValue, CultureInfo.InvariantCulture);
		}
	}
}
